package com.dd.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dd.entity.DdShop;

public class AppMapAction extends BaseAction {
	private  double EARTH_RADIUS = 6378.137;

	private  double rad(double d) {
		return d * Math.PI / 180.0;
	}

	public  double GetDistance(double lat1, double lng1, double lat2,
			double lng2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(
				Math.sqrt(
						Math.pow(Math.sin(a / 2), 2)+ Math.cos(radLat1) * Math.cos(radLat2)* Math.pow(Math.sin(b / 2), 2)
						)
				);
		//asin(sqrt(pow(sin((posx_g-26.076031)/2),2)+cos()*cos()*pow(sin((posy_g-119.28479)/2),2)))
		s = s * EARTH_RADIUS;
		// s = Math.round(s * 10000) / 10000;
		return s * 1000;
	}
	public ActionForward search(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		StringBuffer sbf = new StringBuffer("[");
		try {
			String posx1 = request.getParameter("x1");
			String posx2 = request.getParameter("x2");
			String posy1 = request.getParameter("y1");
			String posy2 = request.getParameter("y2");
			String posx0 = request.getParameter("x0");
			String posy0 = request.getParameter("y0");
			String send="0";
			String book="0";
			String coupon="0";
			System.out.print(posx0+"  "+posy0+"  ");
			if(request.getParameter("isbook")!=null){
				book = request.getParameter("isbook");
			}
			if(request.getParameter("issend")!=null){
				send = request.getParameter("issend");
			}
			if(request.getParameter("iscoupon")!=null){
				coupon = request.getParameter("iscoupon");
			}
			List list = this.getDdShopDAO().findAllByPos_g(posx0,posy0,posx1, posy1, posx2,
					posy2, send, book, coupon);
			for (int i = 0; i < list.size(); i++) {
				DdShop ds = (DdShop) list.get(i);
				String des = "";
				if (ds.getIsbooking() == 1 && ds.getDiscount() > 0F
						&& ds.getDiscount() < 10F) {
					des = ds.getDiscount() + "折";
				} else if (ds.getIssend() == 1) {
					des = ds.getSendreq();
				}
				System.out.print(ds.getId()+" ="+ds.getPosx_g()+"  "+ds.getPosy_g()+"  ");
				System.out.println((int) this.GetDistance(Double.parseDouble(ds
						.getPosx_g()), Double.parseDouble(ds
								.getPosy_g()), Double.parseDouble(posx0), Double.parseDouble(posy0)));
				
				if (i == 0) {
					sbf.append("{\"id\":\"" + ds.getId() + "\",\"name\":\""
							+ ds.getName() + "\",\"px\":\"" + ds.getPosx_g()
							+ "\",\"py\":\"" + ds.getPosy_g() + "\",\"phone\":\""
							+ ds.getPhone() + "\",\"des\":\"" + des
							+ "\",\"url\":\"" + ds.getUrl() + "\",\"address\":\""+ds.getAddress()+"\",\"distance\":\""
							+ (int) this.GetDistance(Double.parseDouble(ds
									.getPosx_g()), Double.parseDouble(ds
									.getPosy_g()), Double.parseDouble(posx0), Double.parseDouble(posy0)) + "\"}");
				} else {
					sbf.append(",{\"id\":\"" + ds.getId() + "\",\"name\":\""
							+ ds.getName() + "\",\"px\":\"" + ds.getPosx_g()
							+ "\",\"py\":\"" + ds.getPosy_g() + "\",\"phone\":\""
							+ ds.getPhone() + "\",\"des\":\"" + des
							+ "\",\"url\":\"" + ds.getUrl() + "\",\"address\":\""+ds.getAddress()+"\",\"distance\":\""
							+ (int) this.GetDistance(Double.parseDouble(ds
									.getPosx_g()), Double.parseDouble(ds
									.getPosy_g()),Double.parseDouble(posx0), Double.parseDouble(posy0)) + "\"}");
				}
			}
			sbf.append("]");
			out.println(sbf.toString());
			out.flush();
			return null;
		} catch (Exception e) {
			out.print("");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
	public ActionForward hotspot(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		StringBuffer sbf = new StringBuffer("[{\"name\":\"鼓楼区\",\"x\":\"26.076128\",\"y\":\"119.28479\"},{\"name\":\"台江区\",\"x\":\"26.108751\",\"y\":\"119.29245\"}]");	
		
		out.println(sbf.toString());
		out.flush();
		return null;
		
	}

}
